import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


service = Service(executable_path=r"C:\Users\yckon\Desktop\chromedriver-win64\chromedriver.exe")


driver = webdriver.Chrome(service=service)

#
qydm_ids = ['03199','15459','15458','15456','15455','15454','15411','15410','15407','15402','15396','15392','15378','15370','15345','15331','15329','15305','15302','15280','15271','15246','15243','15239','15228','15222','15212','15189','15171','15166','15123','15109','15089','15077','15073','15068','15054','15035','14106','14105','14104','14103','14102','14101','14100','14092','14089','14083','14082','14073','14061','14060','14049','14036','14031','14025','14024','14007','14002','13034','13032','13031','13026','13001','12073','12072','12071','12070','12069','12068','12067','12066','12065','12060','12058','12056','12051','12048','12047','12046','12038','12036','12035','12030','12026','12019','12016','12014','12009','12003','11122','11121','11120','11119','11118','11117','11116','11115','11114','11113','11112','11111','11095','11086','11074','11072','11040','11016','10201','10200','10199','10198','10197','10196','10195','10194','10193','10192','10191','10190','10189','10188','10187','10186','10185','10184','10183','10182','10181','10171','10160','10155','10153','10148','10135','10120','10114','10088','10073','10056','10014','10013','10006','09042','09040','09035','09031','09015','09009','09003','08901','08070','08069','08068','08067','08066','08063','08056','08055','08041','08040','08039','08020','08018','08013','08007','08002','08001','07042','07041','07040','07035','07031','07016','07011','07009','07008','07004','07001','06067','06066','06064','06053','06051','06044','06035','06031','06026','06024','06019','06015','06013','06008','06007','06006','06001','05053','05052','05051','05050','05049','05048','05047','05045','05042','05039','05037','05035','05033','05032','05030','05024','05022','05021','05015','05013','05009','05005','05003','04185','04184','04183','04182','04181','04180','04179','04178','04177','04176','04175','04174','04173','04172','04171','04170','04169','04168','04167','04166','04165','04164','04163','04162','04161','04160','04159','04142','04137','04136','04134','04128','04127','04123','04119','04116','04115','04114','04113','04109','04107','04106','04100','04099','04093','04091','04087','04085','04084','04080','04060','04055','04053','04051','04044','04043','04041','04040','04039','04038','04033','04029','04028','04026','04025','04024','04017','04016','04013','04012','03224','03223','03222','03221','03220','03219','03218','03217','03216','03215','03214','03213','03212','03211','03210','03203','03194','03192','03186','03180','03177','03167','03166','03165','03163','03161','03158','03157','03154','03153','03152','03149','03146','03135','03125','03120','03113','03104','03088','03087','03081','03078','03075','03069','03068','03066','03064','03059','03049','03044','03039','03028','03024','03013','03002','02058','02057','02056','02055','02050','02049','02032','02016','02014','02011','02009','02008','02007','02006','02003','01078','01077','01076','01072','01071','01070','01069','01065','01062','01060','01058','01020','01002','01076','01072','01071','01070','01069','01065','01062','01060','01058','01020']


# 创建一个txt文件用于存储结果
with open("output.txt", "w", encoding="utf-8") as file:
    for qydm in qydm_ids:
        # 构建URL
        url = f'http://vdts.ivdc.org.cn:8081/cx/api/cxsj/syscqyinfo/detail?qydm={qydm}'

        # 访问URL
        driver.get(url)

        # 等待页面加载
        time.sleep(2)

        try:
            # 等待页面元素加载完成
            WebDriverWait(driver, 10).until(
                EC.presence_of_element_located((By.TAG_NAME, "body"))
            )

            # 获取页面文本并写入文件
            page_text = driver.find_element(By.TAG_NAME, 'body').text
            file.write(f"Data for qydm={qydm}:\n{page_text}\n\n")
            print(f"Data for qydm={qydm} has been written to file.")

        except Exception as e:
            print(f"Error for qydm={qydm}: {e}")


    driver.quit()